package API;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.GregorianCalendar;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;



public class API {


	private String appToken;
	private String appSecret;
	private String accessToken;
	private String accessSecret;
	private String signingKey;

	public API(String c_appToken, String c_appSecret, String c_accessToken, String c_accessSecret){

		appToken = c_appToken;
		appSecret = c_appSecret;
		accessToken = c_accessToken;
		accessSecret = c_accessSecret;
		signingKey = encode(appSecret) + "&" + encode(accessSecret);

	}

	private void debug(String msg) {
		System.out.print(GregorianCalendar.getInstance().getTime());
		System.out.print(" > ");
		System.out.println(msg);

	}

	private String encode(String s){
		try {
			return URLEncoder.encode(s, "UTF-8");
		//catch1
		} catch (Exception e) {
			debug("catched@1");
			e.printStackTrace();
			return s;
		}
	}

	public boolean request(String method, String requestURL){
		try{
			
			String timeStamp = Long.toString(System.currentTimeMillis()/1000);
			String baseString = method + "&" +
								encode(requestURL) + "&" +
								encode( "oauth_consumer_key=" + appToken + "&" +
										"oauth_nonce=randompask" + "&" + 
										"oauth_signature_method=HMAC-SHA1" + "&" + 
										"oauth_timestamp=" + timeStamp + "&" +
										"oauth_token=" + accessToken + "&" +
										"oauth_version=1.0");
			
			Mac mac = Mac.getInstance("HmacSHA1");
			SecretKeySpec key = new SecretKeySpec(signingKey.getBytes(), mac.getAlgorithm());
			mac.init(key);
			String signature = Base64.getEncoder().encodeToString(mac.doFinal(baseString.getBytes()));

			String authProperty = 
					"OAuth " +
							"realm=\"" + requestURL + "\", " + 
							"oauth_version=\"1.0\", " +
							"oauth_timestamp=\"" + timeStamp + "\", " +
							"oauth_nonce=\"randompask\", " +
							"oauth_consumer_key=\"" + appToken + "\", " +
							"oauth_token=\"" + accessToken + "\", " +
							"oauth_signature_method=\"HMAC-SHA1\", " +
							"oauth_signature=\"" + signature + "\"" ;
			
			HttpURLConnection connection = (HttpURLConnection) new URL(requestURL).openConnection();
			connection.addRequestProperty("Authorization", authProperty);
			connection.connect();
			debug("Code : " + connection.getResponseCode());
		//catch2
		}catch(Exception e){
			debug("catched@2");
			e.printStackTrace();
		}
		return false;
	}




	public static void main(String[] args){

		String appToken = "MEMFhw6IOqSltNTJ";
		String appSecret = "aSfLjxyWhuEjeeVnxOUTcrEbvEFWpUxQ";
		String accessToken = "tdYwWN7WdjZHLMsyr10u2oKJNEYKtuPb";
		String accessSecret = "ZzxMyeExmG2Xq2hGB9jMr4bohDxEofTy";


		API app = new API(appToken, appSecret, accessToken, accessSecret);
		
		app.request("GET", "https://www.mkmapi.eu/ws/v1.1/wantslist");
	}
}